package top.kaoshanji.p1ch02b;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 代码清单 p1ch02b-01：InvocationHandler接口的实现类的示例(2.3.1节)
 * author kaoshanji
 * ^_^
 * create time 2024/12/23 17:56
 */
public class LoggingInvocationHandler implements InvocationHandler {

    private static final Logger logger = Logger.getLogger(LoggingInvocationHandler.class.getName());
    private Object receiverObject;

    public LoggingInvocationHandler(Object thisObject) {
        this.receiverObject = thisObject;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        logger.log(Level.INFO, "调用方法 " + method + "; 参数为 " + Arrays.deepToString(args));
        return method.invoke(receiverObject, args);
    }
}
